home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / PNL Libraries / MyRestartMac.p < prev    next >
Text File  |  1995-03-19  |  2KB  |  84 lines

  1. unit MyRestartMac;
  2.  
  3. interface
  4.  
  5.     procedure RestartMac;
  6.  
  7. implementation
  8.  
  9.     uses
  10.         Processes, GestaltEqu, AppleEvents;
  11.  
  12.     const
  13.         finder_type = 'FNDR';
  14.         finder_creator = 'MACS';
  15.         finder_suite = 'FNDR';
  16.         finder_restart = 'rest';
  17.         finder_shutdown = 'shut';
  18.  
  19.     function GetFinderPSN (var process: ProcessSerialNumber): boolean;
  20.         var
  21.             info: ProcessInfoRec;
  22.             s: str63;
  23.             fs: FSSpec;
  24.             oe: OSErr;
  25.             gv: longInt;
  26.     begin
  27.         GetFinderPSN := false;
  28.         if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
  29.             process.highLongOfPSN := 0;
  30.             process.lowLongOfPSN := kNoProcess;
  31.             info.processInfoLength := sizeof(ProcessInfoRec);
  32.             info.processName := @s;
  33.             info.processAppSpec := @fs;
  34.             while GetNextProcess(process) = noErr do begin
  35.                 if GetProcessInformation(process, info) = noErr then
  36.                     if (info.processType = longInt(finder_type)) and (info.processSignature = finder_creator) then begin
  37.                         GetFinderPSN := true;
  38.                         leave;
  39.                     end;
  40.             end;
  41.         end;
  42.     end;
  43.  
  44.     procedure AECreate (var desc: AEDesc);
  45.     begin
  46.         desc.descriptorType := typeNull;
  47.         desc.dataHandle := nil;
  48.     end;
  49.  
  50.     procedure AEDestroy (var desc: AEDesc);
  51.         var
  52.             junk: OSErr;
  53.     begin
  54.         junk := AEDisposeDesc(desc);
  55.         AECreate(desc);
  56.     end;
  57.  
  58.     procedure SendSimpleEvent (psn: ProcessSerialNumber; class_id, event_id: AEEventID);
  59.         var
  60.             event, reply: AppleEvent;
  61.             err, junk: OSErr;
  62.             target: AEDesc;
  63.     begin
  64.         AECreate(reply);
  65.         err := AECreateDesc(typeProcessSerialNumber, @psn, SizeOf(psn), target);
  66.         err := AECreateAppleEvent(class_id, event_id, target, kAutoGenerateReturnID, kAnyTransactionID, event);
  67.         AEDestroy(target);
  68.         if err = noErr then begin
  69.             junk := AESend(event, reply, kAENoReply + kAEAlwaysInteract, kAENormalPriority, kAEDefaultTimeOut, nil, nil);
  70.         end;
  71.         AEDestroy(event);
  72.         AEDestroy(reply);
  73.     end;
  74.  
  75.     procedure RestartMac;
  76.         var
  77.             process: ProcessSerialNumber;
  78.     begin
  79.         if GetFinderPSN(process) then begin
  80.             SendSimpleEvent(process, finder_suite, finder_restart);
  81.         end;
  82.     end;
  83.  
  84. end.